# Make LaTeX chunk for Table 3

# Load Models and Store AUCs
countries <- c('indo', 'colo')

metrics <- c('dv_var', 'mse', 'r2')
performance <- array(NA,
                     dim = c(length(countries),
                             length(metrics)),
                     dimnames = list(countries,
                                     metrics))
algo <- 'ebma'
for (country in countries) {
  filename <- paste(modeldir,"/",
                    country,
                    "_ebma_",
                    "demean",
                    "_full.RData",
                    sep = "")
  load(filename)
  dv_var_by_year = c()
  mse_by_year = c()
  for (i in 1:length(get(paste(algo,".results",sep="")))) {
    predictions <- as.vector(get(paste(algo,".results",sep=""))[[i]]$fit.oos)
    realizations <- as.vector(get(paste(algo,".results",sep=""))[[i]]$actual.oos)
    dv_var_by_year <- c(dv_var_by_year,
                        mean((realizations - mean(realizations))^2))
    mse_by_year <- c(mse_by_year,
                     mean((realizations - predictions)^2))
  }
  performance[country, 'dv_var'] <- mean(dv_var_by_year)
  performance[country, 'mse'] <- mean(mse_by_year)
  performance[country, 'r2'] <- 1-performance[country,
                                              'mse']/performance[country,
                                                                    'dv_var']
}


# Build Table with Labels
country.labels <- c(indo='Indonesia',
                    colo='Colombia')
metric.labels <- c(dv_var="Var(Dependent Variable)",
                   mse="EBMA mean square error (MSE)",
                   r2="$R^{2}$")

table <- performance
dimnames(table)[[1]] <- country.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- metric.labels[dimnames(table)[[2]]]

# Print to Latex
printA4(table = table,
        filepath = "tables",
        file = "table_A4")

